﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using SharedClassLibrary;

namespace WP8App.ViewModel
{
    internal class MainViewModel
    {
        private string _number1;
        private string _number2;

        public string Number1
        {
            get { return _number1; }
            set
            {
                _number1 = value;
                ExecuteCommand.OnCanExecuteChanged();
            }
        }
        public string Number2
        {
            get { return _number2; }
            set
            {
                _number2 = value;
                ExecuteCommand.OnCanExecuteChanged();
            }
        }
        public IList<OperationType> AvailableOperations { get; set; }
        public OperationType SelectedOperation { get; set; }
        public ObservableCollection<double> CalculatedResults { get; set; }
        public Command ExecuteCommand { get; set; }

        public MainViewModel()
        {
            ExecuteCommand = new Command(ExecuteCommandExecute, ExecuteCommandCanExecute);
            AvailableOperations = new List<OperationType> { OperationType.Addition, OperationType.Substraction };
            CalculatedResults = new ObservableCollection<double>();
        }

        private void ExecuteCommandExecute(object obj)
        {
            if (!ExecuteCommandCanExecute(obj))
            {
                return;
            }

            CalculatedResults.Add(5);
        }

        private bool ExecuteCommandCanExecute(object obj)
        {
            return IsInputValid();
        }

        private bool IsInputValid()
        {
            double result;
            return double.TryParse(Number1, out result) && double.TryParse(Number2, out result);
        }
    }
}
